-
Notifications
You must be signed in to change notification settings - Fork 66
Modified expectedHeaders from TableNode to be in format expected by asse... #118
base: master
Are you sure you want to change the base?
Modified expectedHeaders from TableNode to be in format expected by asse... #118
Conversation
…ssertArrayContains. Response->getHeaders() returns an object, assertArrayContains expect an array, changed for response->getHeaders()->toArray().
They are probably going to ask you to follow project code style:
But looks good to me. |
Great! no problem, if it's necesary I can change the code. |
@victuxbb The problem is that your solution doesn't test header name. Maybe you can implement something like this : /**
* @Then /^the response should contains the following headers:?$/
*/
public function theResponseShouldContainsHeaders(TableNode $headerTable)
{
if (null === $this->response) {
throw new \RuntimeException('You must send a request before testing a response.');
}
$expectedHeaders = $headerTable->getRowsHash();
$existingHeaders = $this->response->getHeaders()->toArray();
foreach ($expectedHeaders as $key => $value) {
if (false === array_key_exists($key, $existingHeaders)) {
throw new \Exception(sprintf(
'No header names "%s" found. "%s" available.',
$key,
implode('", "', array_keys($existingHeaders))
));
}
$real = $existingHeaders[$key];
array_map('trim', $real);
sort($real);
$expected = explode(';', $value);
array_map('trim', $expected);
sort($expected);
$this->getAsserter()->assertArrayEquals($expected, $real);
}
} |
@PedroTroller I was expecting that assertArrayContains is checking the header name and the value and I see that it doesn't. I was trying to understand the code in assertArrayContains but I think it's a little ofuscated :) On the other hand If we discard the use of assertArrayContains by assertArrayEquals your implementation is fine with a few modification: The initial state of the arrays $expectedHeaders and $existingHeaders are for example: Array
(
[Content-Disposition] => attachment; filename="export.csv"
)
Array
(
[Server] => Array
(
[0] => nginx/1.6.2
)
[Content-Type] => Array
(
[0] => application/force-download
)
[Transfer-Encoding] => Array
(
[0] => chunked
)
[Connection] => Array
(
[0] => keep-alive
)
[X-Powered-By] => Array
(
[0] => PHP/5.6.5
)
[Cache-Control] => Array
(
[0] => no-cache, private
)
[Date] => Array
(
[0] => Thu, 12 Feb 2015 17:19:24 GMT
)
[Content-Disposition] => Array
(
[0] => attachment; filename="export.csv"
)
) I changed this line because the 2 arrays for comparing with your code results in this format: $expected = explode(';', $value);
Array
(
[0] => filename="export.csv"
[1] => attachment
)
Array
(
[0] => attachment; filename="export.csv"
) I think there's no need to make an explode of the value... Finally the function can be: /**
* @Then /^the response should contains the following headers:?$/
*/
public function theResponseShouldContainsHeaders(TableNode $headerTable)
{
if (null === $this->response) {
throw new \RuntimeException('You must send a request before testing a response.');
}
$expectedHeaders = $headerTable->getRowsHash();
$existingHeaders = $this->response->getHeaders()->toArray();
foreach ($expectedHeaders as $key => $value) {
if (false === array_key_exists($key, $existingHeaders)) {
throw new \Exception(sprintf(
'No header names "%s" found. "%s" available.',
$key,
implode('", "', array_keys($existingHeaders))
));
}
$real = $existingHeaders[$key];
array_map('trim', $real);
sort($real);
$expected = array($value);
array_map('trim', $expected);
sort($expected);
$this->getAsserter()->assertArrayEquals($expected, $real);
}
} For me is working fine with this code, but I see that there's isn't phpspec for the ApiContext for testing theResponseShouldContainsHeaders, it will be ok if I implement a test? Thanks! |
Explanation here:
#115